<?php

/**
 * Description of FinanceAction
 *
 * @author chacha
 * @date 2016-6-3
 */
class FinanceAction extends CommonAction{
    
    protected $roleflag = 0;
    
    public function __construct() {
        parent::__construct();
        
        Log::write("[FinanceAction::__construct]uid:".$this->uid);
        //雇员or雇主标志
        if ($this->userinfo['utype']==1) {
            $tabTeam = M('team');
            $team = $tabTeam->where("uid=".$this->uid." and state>0")->find();
            if (!$team) {
                $this->roleflag = \App\Common\Constvars::EMFLAG_EE;
            }
            else {
                $this->roleflag = \App\Common\Constvars::EMFLAG_ER;
            }
        }
        else {
            $this->roleflag = \App\Common\Constvars::EMFLAG_ER;
        }
        
        //是否填写真实姓名
        if (ACTION_NAME!='verify' && empty($this->userinfo['realname'])) {
            $this->redirect("/Finance/verify");
        }
    }
    
    public function index() {
        require APP_PATH."/Group/Bridge/Lib/Finance/Finance.class.php";
        $finance = new \Lib\Finance\Finance($this->uid, $this->userinfo, $this->roleflag);
        $finance->process();
    }
    
    //充值申请
    public function charge_ajax() {
        $amount = I('amount');
        if (!$amount) {
            ajaxf("请填写充值金额!");
        }
        
        $now = time();
        $data = array(
            'uid' => $this->uid,
            'amount' => $amount,
            'process' => \App\Common\Constvars::CHARGE_PRO_UNDEAL,
            'ctime' => $now,
            'uptime' => $now,
            'status' => \App\Common\Constvars::STATUS_NORMAL,
        );
        $ret = M('charge_log')->add($data);
        if ($ret===false) {
            ajaxf("申请提交失败！");
        }
        else {
            ajaxs();
        }
    }
    //取消充值申请
    public function cancelcharge_ajax() {
        $cid = I('cid');
        if (!$cid) {
            ajaxf("参数错误!");
        }
        require APP_PATH."/Group/Bridge/Lib/Finance/Cop.class.php";
        $cop = new \Lib\Finance\Cop($this->uid);
        $ret = $cop->cancelcharge($cid);
        if (!$ret) {
            ajaxf();
        }
        else {
            ajaxs();
        }
    }
    
    //提现申请
    public function withdraw_ajax() {
        import('ORG.Util.Verify');
        if (Verify::isEmpty('actype')==false 
            || Verify::isEmpty('acid')==false 
            || Verify::isEmpty('amount')==false) {
            ajaxf("参数错误！");
        }
        
        $now = time();
        $actype = I('actype');//账户类型，支付宝，银行账户
        $acid = I('acid');//账户
        $amount = I('amount');//金额
        
        if (empty($this->userinfo['realname']) || empty($this->userinfo['mobile'])) {
            ajaxf("请先完善用户资料，填写真实姓名和手机号。");
        }
        
        $acserver = '';
        if ($actype==\App\Common\Constvars::ACCOUNT_TYPE_ALIPAY) {
            $acserver = '阿里巴巴';
        }
        else if ($actype==\App\Common\Constvars::ACCOUNT_TYPE_BANKCARD) {
            $bclist = require_once APP_PATH.'Lib/bankList.php';
            $acserver = bankInfo($acid, $bclist);
        }
        
        $withdraw = array(
            'uid' => $this->uid,
            'actype' => $actype,
            'acserver' => $acserver,
            'account' => $acid,
            'opentime' => $now,
            'uptime' => $now,
            'amount' => $amount,
            'process' => \App\Common\Constvars::WITHDRAW_PROCESSING
        );
        
        $ret = M('withdraw_log')->add($withdraw);
        if ($ret===false) {
            ajaxf();
        }
        else {
            ajaxs();
        }
    }
    
    //取消提现申请
    public function cancelwithdraw_ajax() {
        $wid = I('wid');
        if (!$wid) {
            ajaxf("参数错误！");
        }
        
        require APP_PATH."/Group/Bridge/Lib/Finance/Finance.class.php";
        $finance = new \Lib\Finance\Finance($this->uid, $this->userinfo, $this->roleflag);
        if ($finance->cancel_withdraw($wid)) {
            ajaxs();
        }
        else {
            ajaxf();
        }
    }
    
    //催促托管/付款
    public function urgepay_ajax() {
        $jobid = I('job');
        $stageid = I('stage', 0);
        $urgetype = I('urgetype');
        if (!$jobid || !$urgetype) {
            ajaxf("参数错误!");
        }
        require APP_PATH."/Group/Bridge/Lib/Finance/Finance.class.php";
        $finance = new \Lib\Finance\Finance($this->uid, $this->userinfo, $this->roleflag);
        if ($finance->urgepay($jobid, $stageid, $urgetype)) {
            ajaxs();
        }
        else {
            ajaxf();
        }
    }
    
    /**
     * 设置催促信息已读(删除)
     */
    public function delurge_ajax() {
        $urgeid = I('urgeid');
        if (!isset($urgeid) && !empty($urgeid)) {
            $this->ajaxf('参数错误!');
        }
        
        require APP_PATH."/Group/Bridge/Lib/Finance/Finance.class.php";
        $finance = new \Lib\Finance\Finance($this->uid, $this->userinfo, $this->roleflag);
        if ($finance->delurge($urgeid)) {
            $this->ajaxs();
        }
        else {
            $this->ajaxf();
        }
    }
    
    //填写实名页面
    public function verify() {
        $this->display();
    }
    
    //确认雇主已付款
    public function confirmpayed_ajax() {
        $instalment = I('instalment');
        $itemid = I('id',0);
        if (!isset($instalment) || !isset($itemid)) {
            $this->ajaxf('参数错误!');
        }
        
        require APP_PATH."/Group/Bridge/Lib/Finance/Finance.class.php";
        $personal = new \Lib\Finance\Personal($this->uid);
        $item = array('instalment'=>$instalment,'id'=>$itemid);
        if ($personal->confirmpayed($item)) {
            $this->ajaxs();
        }
        else {
            $this->ajaxf();
        }
    }
}
